from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app

# A custom datastore model for associating users with uploaded files.
#from models import UserPhoto

class Printthis(webapp.RequestHandler):
	def get(self):
		self.response.out.write('<html><body>This is blobhandle!</body></html>')
#        self.response.out.write('This is here</body></html>')
#		<input type="button" name="b1" value="Click Me" onclick="location.href='myPage.htm'">

class PhotoUploadFormHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload_photo')
        self.response.out.write(
            '<form method="POST" action="%s">...</form>'
            % upload_url)

class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        try:
            for upload in self.get_uploads():
                user_photo = UserPhoto(user=users.get_current_user(),
                                       blob_key=upload.key())
                db.put(user_photo)
#				self.response.out.write("<p>%s</p>" % user) #added to see what 'user' is
				
            self.redirect('/upload_success.html')

        except:
            self.redirect('/upload_failure.html')

application = webapp.WSGIApplication([('/', Printthis),
									  ('/upload_form', PhotoUploadFormHandler),
                                      ('/upload_photo', PhotoUploadHandler)
                                     ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()